//
//  Registered_TVC.m
//  K-Music
//
//  Created by CHUN on 16/1/3.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "Registered_TVC.h"

@interface Registered_TVC ()<UITextFieldDelegate,UIAlertViewDelegate>
{
    UITextField * _texfiled1;
    UITextField * _texfiled2;
    UITextField * _texfiled3;
}
@end

@implementation Registered_TVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"注册账号";
    
    self.view.backgroundColor = RGB(241, 242, 244);
    
    [self tapView];
    
    [self createUI];
    
}

-(void)createUI{
    
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 10 , WIDTH, 150)];
    
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UILabel *lable = [UILabel lableWithText:CGRectMake(WIDTH/16, 5, WIDTH/3, 40) Color:RGB(4, 4, 4) Text:@"用户昵称" Font:nil];
    [view addSubview:lable];
    
    _texfiled1 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lable.frame), 5, WIDTH*0.75, 40) ];
    _texfiled1.borderStyle = UITextBorderStyleNone;
    _texfiled1.placeholder = @"输入昵称";
    _texfiled1.delegate = self;
    [view addSubview:_texfiled1];
    
    UIView * bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 50, WIDTH, 1)];
    bgview.backgroundColor = RGB(241, 242, 244);
    [view addSubview:bgview];
    
    UILabel *lable_code = [UILabel lableWithText:CGRectMake(WIDTH/16, 55, WIDTH/3, 40) Color:RGB(4, 4, 4) Text:@"设置登陆密码" Font:nil];
    [view addSubview:lable_code];
    
    _texfiled2 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lable_code.frame), 55, WIDTH*0.75, 40) ];
    _texfiled2.borderStyle = UITextBorderStyleNone;
    _texfiled2.placeholder = @"设置登陆密码";
    _texfiled2.secureTextEntry = YES;
    _texfiled2.delegate = self;
    [view addSubview:_texfiled2];
    
    UIView * bgview2 = [[UIView alloc] initWithFrame:CGRectMake(0, 100, WIDTH, 1)];
    bgview2.backgroundColor = RGB(241, 242, 244);
    [view addSubview:bgview2];
    
    
    UILabel *lable3 = [UILabel lableWithText:CGRectMake(WIDTH/16, 105, WIDTH/3, 40) Color:RGB(4, 4, 4) Text:@"重新输入密码" Font:nil];
    [view addSubview:lable3];
    
    _texfiled3 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lable_code.frame),105, WIDTH*0.75, 40) ];
    _texfiled3.borderStyle = UITextBorderStyleNone;
    _texfiled3.placeholder = @"重新输入密码";
    _texfiled3.secureTextEntry = YES;
    _texfiled3.delegate = self;
    [view addSubview:_texfiled3];
    
    UIButton * registbutton = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH/32, CGRectGetMaxY(view.frame)+10 ,WIDTH-WIDTH/16, WIDTH/10)];
    
    [registbutton addTarget:self action:@selector(registered_T) forControlEvents:UIControlEventTouchUpInside];
    [registbutton setTitle:@"确定" forState:UIControlStateNormal];
    registbutton.layer.cornerRadius = 5;
    registbutton.clipsToBounds = YES;
    registbutton.backgroundColor = RGB(240, 176, 39);
    
    [self.view addSubview:registbutton];
    
    //    返回按钮
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
}
- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)registered_T
{
    //匹配验证码跳转
    if ([_texfiled2.text isEqualToString: _texfiled3.text]|| _texfiled2.text.length != 0)
    {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        manager.requestSerializer  = [AFJSONRequestSerializer serializer];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        NSString*infoStr11=[NSString stringWithFormat:@"/api/register"];
        
        NSString *intactURL = [MyBaseUrl stringByAppendingString:infoStr11];
        
        NSDictionary * login = @{@"password":_texfiled2.text,@"phone":[User_information conversion].user_phone,@"activationKey":[User_information conversion].code,@"nickName":_texfiled1.text};
        
        [manager.requestSerializer setValue:@"application/json"
                         forHTTPHeaderField:@"Content-Type"];
        
        [manager.requestSerializer setValue:@"application/json"
                         forHTTPHeaderField:@"Accept"];
        
        [manager POST:intactURL parameters:login success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             NSDictionary * result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
             
             NSLog(@"================:%@",result);
             
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"注册成功,请登录！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
             [alert show];
             
    
             [self.navigationController popToRootViewControllerAnimated:YES];
             
         }failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             [MBProgressHUD showErrorWithStatus:@"注册失败" toView:self.view];
             
             NSLog(@"%@", error);
             
         }];

    }
    else
    {
        [MBProgressHUD showErrorWithStatus:@"输入有误" toView:self.view];
    }
    
}



//收键盘手势
- (void)tapView
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(CancelkeyBoard)];
    [self.view addGestureRecognizer:tap];
}

- (void)CancelkeyBoard
{
    [ _texfiled1 resignFirstResponder];

    [ _texfiled2 resignFirstResponder];
    
    [ _texfiled3 resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
